package com.example.teamtribe.main.club.clubintroduce.media.video;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.example.teamtribe.R;
import com.example.teamtribe.base.BaseActivity;
import com.example.teamtribe.database.Media;
import com.example.teamtribe.event.Jia;
import com.example.teamtribe.main.club.clubintroduce.GridSpacingItemDecoration;
import com.example.teamtribe.main.club.clubintroduce.media.photo.JiaAdapter;
import com.example.teamtribe.utils.Utility;
import com.luck.picture.lib.entity.LocalMedia;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.listener.OnLoadMoreListener;
import com.scwang.smartrefresh.layout.listener.OnRefreshListener;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.util.List;

public class VideoActivity extends BaseActivity {
    private RefreshLayout refreshLayout;
    private Media media;
    private JiaAdapter adapter;
    private RecyclerView recyclerView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_video);
        if(!EventBus.getDefault().isRegistered(this)){
            EventBus.getDefault().register(this);
        }

        media=getIntent().getParcelableExtra("media");
        ((TextView)findViewById(R.id.tv_title)).setText(media.getName());
        ImageView ivCover=(ImageView)findViewById(R.id.iv_cover);
        ((TextView)findViewById(R.id.tv_description)).setText(media.getDescription());
        Glide.with(this).load(Utility.getUri(media.getCover())).into(ivCover);
        refreshLayout = (RefreshLayout)findViewById(R.id.rl_video);
        refreshLayout.setOnRefreshListener(new OnRefreshListener() {
            @Override
            public void onRefresh(RefreshLayout refreshlayout) {
                refreshlayout.finishRefresh(2000/*,false*/);//传入false表示刷新失败
            }
        });
        refreshLayout.setOnLoadMoreListener(new OnLoadMoreListener() {
            @Override
            public void onLoadMore(RefreshLayout refreshLayout) {
                refreshLayout.finishLoadMore(2000/*,false*/);//传入false表示加载失败

            }
        });
        Button upLoad=findViewById(R.id.bt_upload);
        upLoad.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent=new Intent(VideoActivity.this,AddVideoActivity.class);
                intent.putExtra("media",media);
                startActivity(intent);
            }
        });
        recyclerView = findViewById(R.id.rv_video_list);
        recyclerView.setLayoutManager(new GridLayoutManager(this, 3));
        recyclerView.addItemDecoration(new GridSpacingItemDecoration(3, 20, false));
        recyclerView.setNestedScrollingEnabled(false);
        adapter=new JiaAdapter();
        recyclerView.setAdapter(adapter);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        EventBus.getDefault().unregister(this);
    }
    @Subscribe(sticky = true, threadMode = ThreadMode.MAIN)
    public void onEvent(Jia event) {
        List<LocalMedia> list=event.getL();
        for(int i=0;i<list.size();i++){
            adapter.addMedia(list.get(i));
            adapter.notifyDataSetChanged();
        }
    }
}

